/**
 * @author 甜圆
 * @version v1.0.1
 * @date 2025/8/25 15:54
 * @package PACKAGE_NAME
 * @copyright 甜圆
 */

/**
 * (1)	搭建TCP客户端
 * ①	从键盘录入老师信息，录入格式：姓名-年龄-工资，遇quit结束录入
 * zs-31-8900
 * tom-28-7893.5
 * ww-33-4536.5
 * ...
 * quit
 * ②	将每行信息封装成老师对象，全部发送到服务器
 * ③	发送成功后，收取从服务器返回的信息并输出
 * ④	关闭客户端
 * (2)	搭建TCP服务器，端口8002
 * ①	接收客户端发送的所有老师对象，遍历输出，并将老师信息全部写入本地文件 teacher.txt
 * ②	写入完成后，返回给客户端一个信息: "ok"
 * ③	关闭服务器
 */

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TeacherClient {
    public static void main(String[] args) {
        String serverAddress = "localhost";
        int port = 8002;

        try (Socket socket = new Socket(serverAddress, port);
             ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             Scanner scanner = new Scanner(System.in)) {

            List<Teacher> teachers = new ArrayList<>();
            System.out.println("请输入老师信息（格式：姓名-年龄-工资），输入'quit'结束：");

            // 读取键盘输入
            while (true) {
                String line = scanner.nextLine().trim();
                if (line.equalsIgnoreCase("quit")) {
                    break;
                }

                // 解析输入信息
                String[] parts = line.split("-");
                if (parts.length != 3) {
                    System.out.println("格式错误，请重新输入！");
                    continue;
                }

                try {
                    String name = parts[0];
                    int age = Integer.parseInt(parts[1]);
                    double salary = Double.parseDouble(parts[2]);
                    teachers.add(new Teacher(name, age, salary));
                } catch (NumberFormatException e) {
                    System.out.println("年龄或工资格式错误，请重新输入！");
                }
            }

            // 发送老师列表到服务器
            out.writeObject(teachers);
            out.flush();

            // 接收服务器返回的信息
            String response = in.readLine();
            System.out.println("服务器返回：" + response);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
